package com.zou.lotterylucky.data

import android.annotation.SuppressLint
import com.zou.lotterylucky.activity.BetCountEntity
import org.json.JSONObject
import java.io.Serializable
import java.text.SimpleDateFormat
import java.util.*

class Order(obj: JSONObject) : Serializable {
    var machine_number: String? = null
    var id = obj["id"] as Int
    var belongTo = obj["belong_to"] as String
    var serialNumber = obj["serial_number"] as String
    var isWinner = obj["is_winner"] as Int
    var isPaid = obj["is_paid"] as Int
    var isDeleted = obj.getInt("is_deleted")
    var winningAmount = obj.getDouble("total_winning_amount")
    var timeStrAuto = getTimeAuto(obj.getString("created_at"))
    var bets: MutableList<BetCountEntity> = mutableListOf()

    /**
     * @param beijingTime 北京时间的字符串
     * @return 当前手机的时区对应的时间
     */
    @SuppressLint("SimpleDateFormat")
    private fun getTimeAuto(beijingTime: String?): String {
        if (beijingTime != null && beijingTime.length >= 19) {
            val str1 = beijingTime.substring(0, 19).replace("T", " ")
            val format1 = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            format1.timeZone = TimeZone.getTimeZone("GMT+0")
            val date = format1.parse(str1)

            val format2 = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            format2.timeZone = TimeZone.getTimeZone("GMT-4")
            return format2.format(Date(date.time))
        }
        return ""
    }

    fun getTotal(): Double {
        return bets.sumByDouble {
            it.money * getRegionCounts()
        }
    }

    fun getRegionCounts(): Int {
        val regionArray = belongTo.split("+")
        return if (regionArray.isEmpty()) {
            1
        } else {
            regionArray.size
        }
    }
}